<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class Index extends Controller
{
    use \App\Traits\UserTrait, \App\Traits\OauthTrait, \App\Traits\ErrorTrait;

    public function oauthPage(Request $request)
    {
        return view('auth.oauth_page');
    }

    public function login(Request $request)
    {
        $postData = [
            'client_id' => $request->input('client_id'),
            'redirect_uri' => $request->input('redirect_uri'),
            'response_type' => $request->input('response_type'),
            'scope' => $request->input('scope'),
            'state' => $request->input('state'),
            'phone' => $request->input('phone'),
            'pwd' => $request->input('pwd'),
        ];

        $clientData = [
            'client_id' => $postData['client_id'],
        ];
        $result = $this->oauthClientInfo($clientData);
        if (!$result) {
            echo json_encode($this->dataNotExits());exit;
        }

        $loginData = [
            'phone' => $postData['phone'],
            'pwd' => $postData['pwd'],
        ];
        $result = $this->userLogin(self::CODE_USER_LOGIN, $loginData);
        if ($result['succ'] != 1) {
            echo json_encode($this->apiNotSucc(self::CODE_USER_LOGIN, $result));exit;
        }

        $insertData = [
            'client_id' => $postData['client_id'],
            'code' => $this->createCode(),
            'redirect_uri' => $postData['redirect_uri'],
            'user_id' => $result['custId'],
            'user_phone' => $result['accountPhone'],
        ];
        $result = $this->addClientCode($insertData);
        echo json_encode(['result' => 0, 'redirect_url' => $postData['redirect_uri'] . '?code=' . $insertData['code'] . '&state=' . $postData['state']]);
        exit;

    }

    public function oauthToken(Request $request)
    {
        $postData = [
            'grant_type' => $request->input('grant_type'),
            'code' => $request->input('code'),
            'redirect_uri' => $request->input('redirect_uri'),
            'client_id' => $request->input('client_id'),
        ];
        $result = $this->oauthCodeInfo($postData);
        if (!$result) {
            echo json_encode($this->dataNotExits());exit;
        }

        $insertData = [
            'client_id' => $postData['client_id'],
            'access_token' => $this->createToken(),
            'token_type' => $postData['grant_type'],
            'expires_in' => time() + 3600,
        ];
        $result = $this->addToken($insertData);
        echo json_encode($insertData);exit;
    }

    public function getUserInfo(Request $request)
    {
        $token = $request->input('access_token');
        $userId = $request->input('user_id');
        $result = $this->oauthTokenInfo($token);
        if (!$result) {
            echo json_encode($this->dataNotExits());exit;
        }
        $clientId = $result->client_id;
        $userIdInfo = $this->getUserIdInfo($userId, $clientId);
        if (!$userIdInfo) {
            echo json_encode($this->dataNotExits());exit;
        }
        $phone = $userIdInfo->user_phone;
        $data = [
            'phone' => $phone,
        ];
        $result = $this->getUserPhoneInfo(parent::CODE_GET_USER_INFO, $data);
        if ($result['succ'] != 1) {
            echo json_encode($this->apiNotSucc(self::CODE_USER_LOGIN, $result));exit;
        }
        echo json_encode([
            'user_id' => $result['custId'],
            'user_phone' => $data['phone'],
            'user_email' => $result['email'],
        ]);
        exit;
    }
}
